<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\builder\Builder;
use app\BaseController;
use think\facade\Db;
use think\Request;

class Databassystemn extends BaseController
{
    public function show_list(){
        return Builder::table()
            ->setAction('databassystem/page')
//            ->addToolbarDemo('add')
//            ->setRightHeader()
            ->setColumn('input','name','配置名称')
            ->setColumn('input','value','配置值')
            ->setRightHeader()
            ->setButton('edit','修改','databassystem/show_edit')
            ->fetch();
    }
    public function page(){
        $data = Db::name(getTable('database_system'))
            ->select();
        foreach ($data as $key=>$value){
            if($key==0){
                if($value['value']==0){
                    $value['value'] = '关闭';
                }else{
                    $value['value'] = '开启';
                }
            }
//            if($key==1){
//                if($value['value']==0){
//                    $value['value'] = '关闭';
//                }else{
//                    $value['value'] = '开启';
//                }
//            }
            if($key==2){
                $value['value'] = date('Y-m-d',$value['value']);
            }
            $data[$key] = $value;
        }
        $respos['total'] = count($data);
        $respos['data'] = $data;
        return json_page_response($respos);
    }
    public function show_edit(Request $request){
        $id = $request->param('id');
        $data = Db::name(getTable('database_system'))
            ->where('id',$id)
            ->find();
        if($id==1){
            return Builder::form()
                ->setAction('databassystem/edit')
                ->setProject('none','id','',$id)
                ->setProject('switch','value','自动备份',$data['value'])
                ->setButton('submit')
                ->setButton('back')
                ->fetch();
        }
        if($id==2){
            return Builder::form()
                ->setAction('databassystem/edit')
                ->setProject('none','id','',$id)
                ->setProject('input','value','自动备份间隔（天）',$data['value'])
                ->setButton('submit')
                ->setButton('back')
                ->fetch();
        }
        if($id==3){
            return Builder::form()
                ->setAction('databassystem/edit')
                ->setProject('none','id','',$id)
                ->setProject('time','value','上传备份时间',$data['value'])
                ->setButton('submit')
                ->setButton('back')
                ->fetch();
        }
    }
    public function edit(Request $request){
        $data = $request->param();
        if($data['id']==1){
            if($data['value']??null){
                $data['value'] =1;
            }else{
                $data['value'] = 0;
            }
        }
        if($data['id']==3){
            $data['value'] = strtotime($data['value']);
        }
        if(Db::name(getTable('database_system'))->where('id',$data['id'])->update(['value'=>$data['value']])){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
}